﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.HardWareBLL;
using ZK.HardWareIBLL;

namespace ZK.YHP_TransManage.Test
{
    public partial class fangzuobiyi : Form
    {

        FangzuobiyiBLL fangzuobiyiBLL;
        public fangzuobiyi()
        {
            InitializeComponent();
            var ports = SerialPort.GetPortNames();
            comboBox1.DataSource = ports;

            comboBox2.SelectedIndex = 5;
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 1;
            comboBox5.SelectedIndex = 0;

        }
        string tempstr = "";
        private void WeightCallback(byte[] byt)
        {
            this.Invoke(new Action(() =>
            {
                textBox1.AppendText("\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "]"+string.Join("-", byt));
                textBox2.AppendText("\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "]" + Encoding.ASCII.GetString(byt).ToUpper());
            }));

        }
        private void WeightCallbackStr(string byt)
        {
            this.Invoke(new Action(() =>
            {
                textBox1.AppendText("\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "]" + byt);
                //textBox2.AppendText("\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "]" + Encoding.ASCII.GetString(byt).ToUpper());
            }));

        }


        /// <summary>
        /// init
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {

            Common.HaredWareCommonM haredWareCommonM = new Common.HaredWareCommonM();
            haredWareCommonM.PortName = comboBox1.SelectedItem.ToString();
            haredWareCommonM.BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());
            haredWareCommonM.DataBits = Convert.ToInt32(comboBox3.SelectedItem.ToString());
            haredWareCommonM.StopBits = (StopBits)(Convert.ToInt32(comboBox4.SelectedIndex.ToString()));
            haredWareCommonM.Parity = (Parity)(Convert.ToInt32(comboBox5.SelectedIndex.ToString()));
            Common.IConfig.GetSetwagonBalance = haredWareCommonM;
            fangzuobiyiBLL = new HardWareBLL.FangzuobiyiBLL();
            Common.DelegateHelper.A9Event += WeightCallback;
            Common.DelegateHelper.A9EventStr += WeightCallbackStr;
            Common.DelegateHelper.InfoMessageEvent += aa;
            Common.ShowBlackBox.WriteLine("1 init success");
        }

        private void aa(string str)
        {
            this.Invoke(new Action(() =>
            {
                textBox2.AppendText("-"+str+ "\r\n");
            }));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Common.DelegateHelper.A9Event -= WeightCallback;
            Common.DelegateHelper.A9EventStr -= WeightCallbackStr;
            Common.DelegateHelper.InfoMessageEvent -= aa;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Common.DelegateHelper.A9Event += WeightCallback;
            Common.DelegateHelper.A9EventStr += WeightCallbackStr;
            Common.DelegateHelper.InfoMessageEvent += aa;
        }
    }
}
